/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package login;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author barre
 */
public class ArticulosServlet extends HttpServlet {
    private final static String LOGIN_NAME_ATTRIBUTE ="loginName";

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map errors = new HashMap();
        String url = "/MainPageRefresh.jsp";
        HttpSession session = request.getSession(false);
        //Integer descargas = (Integer) session.getAttribute("downloads");
        if(LoginManager.authenticated(request)){
            String file = (String) request.getParameter("file");
            Articulos arts = new Articulos();
            ArticuloForm art = arts.getArticuloByFile(file);
            session.setAttribute("fileArt", art);
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
            dispatcher.forward(request, response);
        }else{
            errors.put(LOGIN_NAME_ATTRIBUTE, "Usuario Anónimo. Inicie sesión o registrese previamente");
            if (!errors.isEmpty()) {
              session.setAttribute("errors", errors);
            }
            //response.sendRedirect(response.encodeRedirectURL("MainPage.jsp"));
            /*response.setHeader("Pragma", "no-cache");
            response.setHeader("Expires", "Sat, 6 May 1995 12:00:00 GMT");  
            response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");  
            response.addHeader("Cache-Control", "post-check=0, pre-check=0");*/
            url="MainPage.jsp";
            response.sendRedirect(url);
            /*RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
            dispatcher.forward(request, response);*/
        }

        /*RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
        dispatcher.forward(request, response);*/
        //response.sendRedirect(url);
    }
}
